/**
 * @name 登录js 的类
 * @author Yang
 * @date 2018-03-29
 * @version 1.0.1
 */
var Login=function () {

    var actionHandler= function () {
        $('#cap').on("click",function () {
            $(this).attr("src",base+"/common/get_img.html?timestamp="+(new Date().valueOf()))
        })
    }

    var publicKeyHandler= function () {
        $('form').validate({
            rules:{
                username:"required",
                password:"required",
                code:"required"
            },
            messages:{
                username:{
                    required:"必填"
                },
                password:{
                    required:"必填"
                },
                code:{
                    required:"必填"
                }
            },
            submitHandler:function (form) {
                $.ajax({
                    type:"get",
                    url:base+"/common/public_key",
                    dataType:"json",
                    success:function (data) {
                        var password= $('#password').val();
                        var rsaKey= new RSAKey();
                        rsaKey.setPublic(b64tohex(data.modulus), b64tohex(data.exponent));
                        var enPassword = hex2b64(rsaKey.encrypt(password));
                        $('#enPassword').val(enPassword);
                        console.log(enPassword);
                        $("form").ajaxSubmit({
                            dataType:"json",
                            success:function (data) {
                                if (data.success){
                                    setTimeout(function () {
                                        location.href=base+'/manage/welcome/index.html';
                                    },1500)
                                    return;
                                }
                                $("#cap").click();
                                Common.alert(data);
                            }
                        })
                    }
                })
            }
        });

    };

    /**
     * 修改密码
     *
     */
    var changeHandler=function f() {
        var form= $('form');
        form.validate({
            rules:{
                password:{
                    required:true,
                    minlength:6,
                    maxlength:20
                },
                endPassword:{
                    required:true,
                    minlength:6,
                    maxlength:20
                },
                enPass:{
                    required:true,
                    equalTo:"#endPassword"
                }
            },
            messages:{
                password:{
                    required:"必填",
                    minlength:"至少6位数",
                    maxlength:"最多20位数"
                },
                endPassword:{
                    required:"必填",
                    minlength:"至少6位数",
                    maxlength:"最多20位数"
                },
                enPass:{
                    required:"必填",
                    equalTo:"两次输入的密码不相同"
                }
            },
            submitHandler:function (data) {
                $.ajax({
                    type:"get",
                    url:base+"/common/public_key",
                    dataType:"json",
                    success:function (data) {
                        var password= $('#password').val();
                        var rsaKey= new RSAKey();
                        rsaKey.setPublic(b64tohex(data.modulus), b64tohex(data.exponent));
                        password = hex2b64(rsaKey.encrypt(password));
                        var endpassword=$("#endPassword").val();
                        endpassword=hex2b64(rsaKey.encrypt(endpassword));
                        var params={};
                        params.password= password;
                        params.endPass=endpassword;
                        params= JSON.stringify(params);
                        $.ajax({
                            type:"post",
                            url:"change",
                            data:{params:params},
                            dataType:"json",
                            headers:{"token":token},
                            success:function (data) {
                                Common.alert(data);
                                if (data.success){
                                    setTimeout(function () {
                                        location.reload(true);
                                    },1500)
                                }
                            }
                        })
                    }
                });
            }
        })
    };

    return{
        init:function () {
           publicKeyHandler();
           actionHandler();
        },
        change:function () {
            changeHandler();
        }
    }
}();
